<?php

namespace App\Models\Activity;

use Carbon\Carbon;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\Redis as Redis;

class ActivityCustomer extends Model
{

    protected $table = 'activity_customer';

    protected $primaryKey = 'activity_id';

    public $timestamps = false;

    public $incrementing = false;

    /**
     * 查询当前正在生效的会员活动
     * @param int $activity_type 活动类
     * @return array|null
     */
    public static function getCache( $activity_type ) {

        if ( empty($activity_type) || !in_array($activity_type, array(30,31,32,33) )) {
            return null;
        }

        $redis_index = 'WDH_ACTIVITY_CUSTOMER_' . $activity_type;

        //从redis中获取缓存数据
        $return_data = Redis::get($redis_index);
        if ($return_data) {
            return json_decode($return_data, true);
        }

        $where = [
            ['start_time', '<=', Carbon::now()],
            ['end_time', '>=', Carbon::now()],
            ['activity_type', $activity_type],
            ['useFlg', 1]
        ];
        $custom_activity_data = self::where($where)->get();

        $return_data = [];

        if ( $custom_activity_data ) {

            foreach ( $custom_activity_data as &$item ) {

                $activity = json_decode($item['activity_json'], true);

                switch ($activity_type) {

                    case 32: //充值有礼

                        $recharge_amount = $activity['recharge_amount'];//充值金额

                        $prize_name = [];
                        foreach ($activity['prize'] as $prize) {
                            if ( $prize['prize_type'] == 1 ) {
                                $prize_name[] = '充值' . $recharge_amount . '元送' . $prize['points'] . '积分';
                            } else if ( $prize['prize_type'] == 2 ) {
                                $prize_name[] = '充值' . $recharge_amount . '元送' . $prize['card_name'];
                            } else if ( $prize['prize_type'] == 3 ) {
                                $prize_name[] = '充值' . $recharge_amount . '元送' . $prize['balance'] . '元';
                            }
                        }
                        $return_data[] = [
                            'id' => $item['activity_id'],
                            'name' => $item['activity_name'],
                            'start_time' => $item['start_time'],
                            'end_time' => $item['end_time'],
                            'recharge_amount' => $recharge_amount,
                            'platform' => $activity['platform'], //充值平台，[1,2,3]
                            'reward_cycle' => $activity['reward_cycle'], //奖励周期 0、首次奖励  1、重复奖励
                            'prize' => $activity['prize'],
                            'prize_name' => $prize_name
                        ];

                        break;

                    default:
                        break;

                }

            }

        }

        Redis::setex($redis_index, 7200, json_encode($return_data, true));

        return $return_data;

    }


}